/*
Copyright (C) 2010 
Author		  Oliver Schenk
Contact		  Oliver.Schenk76@gmail.com

This program is free software; you can redistribute
it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
This program is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; 
if not, see <http://www.gnu.org/licenses/>.
*/

<%@ page language="java" import="java.util.*,java.text.*"%>
<%@ page import="java.net.MalformedURLException" %>
<%@ page import="java.net.URL" %>
<%@ page import="com.google.gdata.client.calendar.*" %>
<%@ page import="com.google.gdata.data.calendar.*" %>
<%@ page import="com.google.gdata.client.*" %>
<%@ page import="com.google.gdata.data.*" %>
<%@ page import="com.google.gdata.model.*" %>
<%@ page import="com.google.gdata.util.*" %>
<%@ page import="com.google.gdata.data.calendar.*" %>
<%@ page import="com.google.gdata.data.extensions.*" %>
<%@ page import="com.google.gdata.data.*" %>
<%@ page import="com.google.gdata.client.http.*" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="SelfBookingSystem.AdminValues" %>
<%@ page import="SelfBookingSystem.PMF" %>
<%@ page import="org.apache.commons.configuration.*" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>

<%!
public int nullIntconv(String inv)
{   
	int conv=0;
		
	try{
		conv=Integer.parseInt(inv);
	}
	catch(Exception e)
	{}
	return conv;
}

%>
<!--If this page is not called embedded in index.php forward to index.php-->
<script type="text/javascript">
if(window.location.pathname.indexOf("index") == -1){
	window.location.href = "index.jsp";
}
</script>
<%
//Variablen Deklaration
String GoogleCalendardURL;
String GoogleEmail;
String Password;
String tmp;
String value;
String checked;
String BadCredentials = new String();;
String Credential = new String();
String AppID = new String();
   
    
ArrayList HTMLForm = new ArrayList();
ArrayList HTMLFormDescription = new ArrayList();
ArrayList HTMLFormHint = new ArrayList();
ArrayList HTMLFormName = new ArrayList();
ArrayList HTMLFormValue = new ArrayList();

//Get Userdata
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();	



Credential = request.getParameter("GoodCredentials");   	

   
if(Credential != null){   	
	if(Credential.contains("false")){
		BadCredentials = "<p>BadCredentials</p>";
	}else if(Credential.contains("true")){
		BadCredentials = "<p>GoodCredentials</p>";
	}
}else{
	BadCredentials = "<p>&nbsp;</p>";
}

if(request.getParameter("AppID") != null){
	AppID = request.getParameter("AppID");
}else{
	AppID = "xxx";
}
System.out.print("AppID "+ AppID);

Map<String, String> m = SelfBookingSystem.GetConfigurationValues.GetConfigurationValues(AppID);



try
{
    XMLConfiguration fields = new XMLConfiguration("config/AdminFields.xml");
    // do something with config
   	System.out.println("JSP: Field[0]: " + fields.getString("tables.table.fields.field(1).name"));

List list = fields.configurationsAt("tables.table.fields.field");
	int i=0;
	for(Iterator it = list.iterator(); it.hasNext();)
	{
		tmp=null;
	    HierarchicalConfiguration sub = (HierarchicalConfiguration) it.next();
		value="";
		if(m.get(sub.getString("HTMLField.name")) != null){
			value = m.get(sub.getString("HTMLField.name"));
		}else if(sub.getString("HTMLField.value") != null){
			value = sub.getString("HTMLField.value");
		}else{
			value ="";
		}

		if(sub.getString("HTMLField.type").contains("text") || sub.getString("HTMLField.type").contains("password")){
			tmp = "<input type=\""+ sub.getString("HTMLField.type") + "\" "; 
			tmp += "name=\"" + sub.getString("HTMLField.name") +"\" ";
			tmp += "size=\""+ sub.getString("HTMLField.size") +"\" "; 
			tmp += "value=\""+ value +"\" ";
			tmp += "class=\""+ sub.getString("HTMLField.class") +"\" ";
			if (sub.getString("HTMLField.readonly").contains("true")){
				tmp += "readonly ";  
			}
			tmp += "maxlength=\""+sub.getString("HTMLField.maxlength")+"\">";
		}
		if(sub.getString("HTMLField.type").contains("hidden")){
			if(sub.getString("HTMLField.name").contains("AppID")){
				tmp = AppID;
			}else{
				tmp = "";
			}
			if(sub.getString("HTMLField.name").contains("User")){
				value = user.getNickname();
			}
			tmp += "<input type=\""+ sub.getString("HTMLField.type") + "\" "; 
			tmp += "name=\"" + sub.getString("HTMLField.name") +"\" ";
			tmp += "value=\""+ value +"\">";
		}
		
		if(sub.getString("HTMLField.type").contains("radio")){
			List listRadio = fields.configurationsAt("tables.table.fields.field.HTMLField.option");
			tmp = "";
			for(Iterator itRadio = listRadio.iterator(); itRadio.hasNext();)
			{

	    		HierarchicalConfiguration subRadio = (HierarchicalConfiguration) itRadio.next();
	    		System.out.print("XML Value " + subRadio.getString("value"));
	    		System.out.print("Value " + value);
	    		
				if (subRadio.getString("value").contains(value)){
					checked = "checked";
				}else{
					checked = "";
				}			
				tmp += "<input type=\""+ sub.getString("HTMLField.type") + "\" "; 
				tmp += "name=\"" + sub.getString("HTMLField.name") +"\" ";
				tmp += "value=\"" + subRadio.getString("value") + "\" " + checked + "> " + subRadio.getString("text") + "<br>";
			}	
		}
		 
		HTMLForm.add(tmp);
		HTMLFormDescription.add(sub.getString("HTMLField.description"));
		HTMLFormHint.add(sub.getString("HTMLField.hint"));
		HTMLFormName.add(sub.getString("HTMLField.name"));
		i++;   
	}
}

catch(ConfigurationException ex)
{
    // something went wrong, e.g. the file was not found
}


%>
<%=BadCredentials%>
	<p>
	</p>
	<form action="/StoreAdminValues" onsubmit="return validateFormOnSubmit(this)" method="post">
	<table border=1>
<%
	for(int k=0; k<HTMLForm.size(); k++){
	//Show only if HTML field is not hidden
	if(HTMLForm.get(k).toString().indexOf("hidden") == -1){
	
%>
	
		<tr><td>
		<%=HTMLFormName.get(k)%> 
		<% if (HTMLFormHint.get(k).toString().compareTo("") > 0){%>
		<a href="javascript:showHint(<%=k%>,<%=HTMLForm.size()%>)">?</a>
		<%}%>
		</td><td>
		<%=HTMLForm.get(k)%>
		</td><td>
		<%=HTMLFormDescription.get(k)%>
		</td>
				
		<% if (k==0){%>
			<td rowspan="<%=HTMLForm.size()%>" valign="top">
			<%for(int i=0; i<HTMLForm.size(); i++){%>
				<div style="display: none;" id="<%=i%>">
	   			<%=HTMLFormHint.get(i)%>
				</div>
			<%}%>
			</td>	
		<%}%>
		</tr>
<%
		}else{
%>		
			<%=HTMLForm.get(k)%>
<%			
		}
	}
%>
      <tr><td colspan="2"><div><input type="submit" value="Save Values" /></div></td></tr>
    </table>
    </form>

